package com.example.smartcity.ui.Adapter;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.smartcity.R;

import java.util.List;

public class FragmentNewsListViewAdapter extends ArrayAdapter {
    int Resource;
    public FragmentNewsListViewAdapter(@NonNull Context context, int resource, @NonNull List<initNewsItem> lists) {
        super(context, resource, lists);
        Resource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        initNewsItem item = (initNewsItem) getItem(position);
        View view =null;
        ViewHolder viewHolder =null;
        if (convertView==null){
            view = LayoutInflater.from(parent.getContext()).inflate(Resource,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.IV_NewsPhoto = view.findViewById(R.id.IV_NewsPhoto);
            viewHolder.TV_NewsTitle = view.findViewById(R.id.TV_NewsTitle);
            viewHolder.TV_Message = view.findViewById(R.id.TV_Message);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.TV_NewsTitle.setText(item.getTitle());
        viewHolder.TV_Message.setText(item.getContent());
        RequestOptions requestOptions = new RequestOptions().override(103);
        Glide.with(parent.getContext()).load(item.getPhotoPath()).apply(requestOptions).into(viewHolder.IV_NewsPhoto);
        return view;
    }

    class ViewHolder{
        ImageView IV_NewsPhoto;
        TextView TV_NewsTitle;
        TextView TV_Message;
    }
}
